import { MapGenerator, renderGround, renderUsefulWall } from "game-algorithm";
import { Room } from "game-algorithm/src/MapGenerator/utils/Room";
import { sample, sampleSize } from "game-random";
import { chunk, partition, zip } from "lodash-es";
import Chests from "./Chest";
export default class GameMap extends MapGenerator {
    scale = 100;
    get maxWidth() {
        return this.map[0].length * this.scale;
    }
    get maxHeight() {
        return this.map.length * this.scale;
    }
    /**随机挑一个空白的位置**/
    pickPlace() {
        const room = sample(this.rooms, this.seed);
        return sample(room.tiles, this.seed).entries();
    }

    chests!: Chests;
    chestNumber = 20;

    /** 创建宝箱 */
    createChest(prefab: Laya.Prefab) {
        this.chests = new Chests(prefab, this.chestNumber);

        const allChunks = zip(
            ...chunk(this.chests.allChest, this.rooms.length)
        ) as Laya.Sprite[][];

        allChunks.forEach((chests, index) => {
            const pos = sampleSize(
                this.rooms[index].edgeTiles,
                chests.length,
                this.seed + index
            );
            pos.forEach((ii, index1) => {
                chests[index1].pos(
                    ii.tileX * this.scale,
                    ii.tileY * this.scale
                );
            });
        });
        return this.chests.allChest;
    }
    Walls!: Laya.Sprite[];
    createWalls(Wall: Laya.Prefab, WallFake: Laya.Prefab) {
        // console.log(this.map.map);
        const Walls: Laya.Sprite[] = [];
        this.render(
            renderUsefulWall(
                (map, x: number, y: number) => {
                    const target: Laya.Sprite = WallFake.create();
                    target.pos(x * target.height, y * target.height);
                    Walls.push(target);
                },
                (map, x: number, y: number) => {
                    const target: Laya.Sprite = Wall.create();
                    // 墙是没有摩擦力的
                    const rb = target.getComponent(Laya.BoxCollider)!;
                    rb.friction = 0;
                    target.pos(x * target.height, y * target.height);

                    Walls.push(target);
                }
            ),
            renderGround((map, x, y) => {
                // 地面需要赋予摩擦力
                const target: Laya.Sprite = Wall.create();
                target.pos(x * target.height, y * target.height);
                Walls.push(target);
            })
        );
        this.Walls = Walls;
        return Walls;
    }
}
